<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="角色名">
        <el-select v-model="ruleForm.RoleID" placeholder="请选择角色">
      <el-option :label="item.Rname"  v-for="(item,index) in role"  :key="index" :value="item.Rid"></el-option>
    </el-select>
    </el-form-item>

            <el-form-item label="姓名" prop="UserName">
                <el-input v-model="ruleForm.UserName"></el-input>
            </el-form-item>
            
            <el-form-item label="性别" prop="Sex">
                <el-radio-group v-model="ruleForm.Sex">
                    <el-radio label="男" name="sex" ></el-radio>
                    <el-radio label="女" name="sex"></el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="出生日期" required>
                <el-col :span="11">
                    <el-form-item prop="Birthday">
                        <el-date-picker value-format="yyyy-MM-dd" placeholder="选择日期" v-model="ruleForm.Birthday"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>

            <el-form-item label="手机号码" prop="Phone">
                <el-input v-model="ruleForm.Phone"></el-input>
            </el-form-item>
            <el-form-item label="账户" prop="LoginName">
                <el-input v-model="ruleForm.LoginName"></el-input>
            </el-form-item>
                       <el-form-item label="状态" prop="Status">
                <el-switch v-model="ruleForm.Status" ></el-switch>
            </el-form-item>
           
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>


  
  
    </div>
    
</template>

<script>

export default {
    name:'update',
    data(){
        return {
            role:[],
            ruleForm: {
                UserName: "",
                Sex:"",
                Birthday: "",
                ImgID:1,
                RoleID:1,
                Phone: "",
                LoginName: "",
                Status:true
            },
            rules: {
                UserName: [
                    { required: true, message: '请输入用户信息', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                RoleID: [
                    { required: true, message: '请选择角色', trigger: 'change' }
                ],
                Birthday: [
                    {required: true, message: '请选择日期', trigger: 'change' }
                ],
                Sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                Phone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在必须为11位', trigger: 'blur' }
                ],
                LoginName: [
                    { required: true, message: '请输入账户', trigger: 'blur' }
                ],
                LoginPwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ],
            }
        }
    },
    props:{dialogUpdate:Boolean,data:Object},
    methods:{
        submitForm(formName) {
            //先验证表单数据
            this.$refs[formName].validate((valid) => {
                if (valid) {
                  
               this.axios({
                method:'put',
                url:'User/Update1',
                data:this.ruleForm
            }).then(res=>{
                if(res.code>0){
                 
                  this.$emit('update:dialogUpdate',false)
                    this.$message({ type:'success', message:res.msg})

                }else{
                    this.$message({ type:'danger', message:res.msg})
                }
            })
             
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
       
    },
    created(){
        this.axios({
            method:'get',
            url:'Role',
            params:null,
        }).then(res=>{
           this.role=res.data
       
        })
        this.ruleForm = this.data;
        this.ruleForm.Birthday = new Date(this.ruleForm.Birthday); 
       

    }, watch: {
        dialogUpdate(newvalue,oldvalue){
            //修改数据的值
            this.ruleForm = this.data;
            this.ruleForm.Birthday = new Date(this.ruleForm.Birthday);
        }
    }
}
</script>

<style scoped>

</style>